<template>
	<view class="container">
		<cu-custom  :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">
				修改密码
			</block>
		</cu-custom>
		
		<view class="page-form">

			<u-form :model="formData" ref="uForm" label-width="140" :border-bottom="false"
				:label-style="{fontSize:'28rpx'}">
				<u-form-item label="原密码" prop="oldPassword" required>
					<u-input v-model="formData.oldPassword" type='password' placeholder="请填写原密码"></u-input>
				</u-form-item>
				<u-form-item label="重置密码" prop="newPassword" required>
					<u-input v-model="formData.newPassword" type='password' placeholder="请填写重置密码"></u-input>
				</u-form-item>
				<u-form-item label="确认密码" prop="confirmPassword" required>
					<u-input v-model="formData.confirmPassword" type='password' placeholder="请填写确认密码"></u-input>
				</u-form-item>

				<u-button @click="$stopClicks.noMultipleClicks(submitForm)" type="primary"
					style="margin-top: 35px;">提交</u-button>
			</u-form>
		</view>
		
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		editMyPassword
	} from '@/api/auth.js';
	import {
		mapGetters
	} from "vuex"
	import md5Libs from "uview-ui/libs/function/md5";
	import {
		getLogout
	} from '@/api/auth.js';
	export default {
		computed: {
			...mapGetters(["userInfo"]),
		},
		data() {
			return {
				formRules: {
					oldPassword: [{
						required: true,
						message: '请填写原密码',
						trigger: 'blur'
					}],
					newPassword: [{
						required: true,
						message: '请填写重置密码',
						trigger: 'blur'
					}],
					confirmPassword: [{
							required: true,
							message: '请填写确认密码',
							trigger: 'blur'
						},
						{
							asyncValidator: (rule, value, callback) => {
								if (value != this.formData.newPassword) {
									return callback(new Error("重置密码和确认密码不一致"));
								} else {
									return callback();
								}
							},
							trigger: ['blur'],
						}
					],
				},
				formData: {
					oldPassword: "",
					newPassword: "",
					confirmPassword: "",
				}
			};
		},
		methods: {
			submitForm() {
				let that = this;
				this.$refs.uForm.validate(valid => {
					if (valid) {
						let userinfo = uni.getStorageSync("userInfo");
						let params = {
							pwd: this.formData.newPassword,
							oldPwd:this.formData.oldPassword,
							seatId:userinfo.id
						}
						
						editMyPassword(params).then((res) => {
							let {data} = res;
							
							if (data.code == 200) {
								uni.setStorageSync("token",'');
								uni.removeStorage("token");
								that.$store.dispatch("setUserInfo",  {});
								uni.clearStorageSync();
								
								
								that.$refs.uToast.show({
									title: data.msg,
									type: 'success',
									message:data.msg,
									duration: 3000,
									url:'/pages/index/login'
								})
							} else {
								that.$refs.uToast.show({
									title: data.msg,
									type: 'error',
									message:data.msg,
									duration: '2000'
								})
							}
						})

					}
				})
			},
		},
		onReady() {
			this.$refs.uForm.setRules(this.formRules);
		}
	}
</script>

<style lang="scss">
.container {
	width: 100%;
	color: #333333;
	background-image: linear-gradient(to bottom, #075AFF, #ffffff);
	background-size: 100% 45%;
	background-repeat: no-repeat;
	background-position: top center;
	overflow-y: scroll;
}
</style>